﻿/*  
 * 作者: Elan
 * 创建时间: 2013-5-2 20:49:36  
 
 * 说明:   采购信息主窗体
 */
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevComponents.AdvTree;
using LL.DevComponents;
using LL.Utility.WindowsForms;
using NanoERP.Business.Permission;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;
using NanoERP.Modules.SystemMgmt;

namespace NanoERP.LL_BaseInfo
{
    [Module("FrmMgrUser")]
    public partial class FrmMgrUser : ModuleForm
    {
        #region 属性

        private SelectedType _selectedType;
        private SelectedType SelectedType
        {
            get { return _selectedType; }
            set
            {
                _selectedType = value;
                ChangeUiState();
            }
        }

        #endregion

        #region 构造

        public FrmMgrUser()
        {
            InitializeComponent();
            InitControls();
            if (DesignMode)
                return;
            LoadData();
        }

        #endregion

        #region 私有方法

        private void LoadData()
        {
            this.treeView1.Nodes.Clear();
            var list = new UserService().GetDepTreeWithUsers();
            var parent = this.treeView1.Nodes;
            BuildNode(parent, list);
            if (this.treeView1.Nodes[0] != null)
                this.treeView1.Nodes[0].ExpandAll();
        }

        private void ReLoadData()
        {
            var path = treeView1.SelectedNode.FullPath;
            LoadData();
            var node = treeView1.FindNodeOnPath(path);
            treeView1.SelectedNode = node;
        }

        private void InitControls()
        {
            InitToolStrips();
            InitTreeView();
            ChangeUiState();
        }

        private void InitToolStrips()
        {
            tsAddDep.Click += new EventHandler(tsAddDep_Click);
            tsAddUser.Click += new EventHandler(tsAddUser_Click);
            tsAuth.Click += new EventHandler(tsAuth_Click);
            tsEdit.Click += new EventHandler(tsEdit_Click);
            tsDel.Click += new EventHandler(tsDel_Click);
        }

        private void InitTreeView()
        {
            treeView1.ImageList = this.IconList;
            treeView1.AfterNodeSelect += new AdvTreeNodeEventHandler(treeView1_AfterNodeSelect);
            treeView1.SelectedIndexChanged += new EventHandler(treeView1_SelectedIndexChanged);
            //var tvHelper = new TreeViewHelper(this.treeView1);
            //tvHelper.HandleDragDrop();
            //tvHelper.DropCompleted += tvHelper_DropCompleted;
            //tvHelper.EnableCheckAll();
        }

        private void ChangeUiState()
        {
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                item.Enabled = false;
            }
            switch (this.SelectedType)
            {
                case SelectedType.Department:
                    tsAddDep.Enabled = true;
                    tsAddUser.Enabled = true;
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    break;
                case SelectedType.User:
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    tsAuth.Enabled = true;
                    tsRole.Enabled = true;
                    break;
                default:
                    tsAddDep.Enabled = true;
                    tsAddUser.Enabled = true;
                    break;
            }
        }

        #endregion

        #region 静态方法

        private void BuildNode(NodeCollection parent, IList<DepartmentTree> list)
        {
            foreach (var departmentModel in list)
            {
                var node = new Node
                {
                    Text = departmentModel.Name,
                    Tag = departmentModel,
                    ImageKey = "folder_classic_opened.png"
                };
                BuildNode(node.Nodes, departmentModel.Children);
                //BuildUserNode(node.Nodes, departmentModel.Users);
                parent.Add(node);
            }
        }

        #endregion

        #region 事件处理

        void treeView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void treeView1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            var dep = e.Node.Tag as DepartmentTree;
            if (dep == null)
                return;
            gvList.DataSource = dep.Users;
        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //if (e.Node == treeView1.Nodes[0])
            //{
            //    SelectedType = SelectedType.None;
            //    return;
            //}
            //if (panel1.Controls.Count > 0)
            //    panel1.Controls[0].Dispose();
            //panel1.Controls.Clear();

            //if (e.Node.Tag as DepartmentTree != null)
            //{  //BindDepartment(e.Node.Tag as DepartmentTreeModel);
            //    SelectedType = SelectedType.Department;

            //    var frm = new FrmEditDepartment()
            //    {
            //        TopLevel = false,
            //        Data = e.Node.Tag as SYS_Department,
            //        Dock = DockStyle.Fill,
            //        FormBorderStyle = FormBorderStyle.None
            //    };
            //    frm.Enabled = false;
            //    panel1.Controls.Add(frm);
            //    frm.Show();
            //    return;
            //}
            //if (e.Node.Tag as UserModel != null)
            //{  //BindUser(e.Node.Tag as UserModel);
            //    SelectedType = SelectedType.User;

            //    var frm = new FrmEditUser()
            //    {
            //        TopLevel = false,
            //        Data = e.Node.Tag as UserModel,
            //        Dock = DockStyle.Fill,
            //        FormBorderStyle = FormBorderStyle.None
            //    };
            //    frm.Enabled = false;
            //    panel1.Controls.Clear();
            //    panel1.Controls.Add(frm);
            //    frm.Show();
            //    return;
            //}
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.CancelEdit || e.Label == null)
                return;
            if (e.Node.Tag is DepartmentTree)
            {
                var dep = e.Node.Tag as DepartmentTree;
                dep.Name = e.Label.Trim();

                new DepartmentService().UpdateDepartment(dep);
            }
        }

        private void tvHelper_DropCompleted(object sender, DropCompleateArgs e)
        {

            TreeNode destinationNode = e.DestinationNode;
            TreeNode sourceNode = e.SourceNode;
            if (destinationNode.Tag is UserModel
                || !MessageDialogHelper.ConfirmYesNo("确认移动？"))
            {
                e.Cancel = true;
                return;
            }
            var dep = destinationNode.Tag as DepartmentTree;
            var dep2 = sourceNode.Tag as DepartmentTree;
            var user = sourceNode.Tag as UserModel;
            Guid? desId = dep == null ? null : new Guid?(dep.Id);
            if (dep2 != null)
            {
                using (var depBo = new DepartmentService())
                    depBo.Move(dep2.Id, desId);
                return;
            }
            new UserService().MoveToDepartment(user.Id, desId);
        }

        #endregion

        #region 工具栏操作

        private void tsAddDep_Click(object sender, EventArgs e)
        {
            var dep = treeView1.SelectedNode.Tag as SYS_Department;
            if (dep == null)
                return;
            var frm = new FrmEditDepartment()
            {
                ParentId = dep.Id
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
                ReLoadData();
        }

        private void tsAddUser_Click(object sender, EventArgs e)
        {
            var dep = treeView1.SelectedNode.Tag as SYS_Department;
            if (dep == null)
                return;
            var frm = new FrmEditUser()
            {
                DepartmentId = dep.Id
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
                ReLoadData();
        }

        private void tsEdit_Click(object sender, EventArgs e)
        {
            if (SelectedType == SelectedType.Department)
            {
                var frm = new FrmEditDepartment()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_Department
                };
                if (frm.ShowDialog(this) == DialogResult.OK)
                    ReLoadData();
            }
            else
            {
                var frm = new FrmEditUser()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_User
                };
                if (frm.ShowDialog(this) == DialogResult.OK)
                    ReLoadData();
            }
        }

        private void tsAuth_Click(object sender, EventArgs e)
        {
            var frm = new FrmSelectUserFuncs()
            {
                Data = treeView1.SelectedNode.Tag as SYS_User
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
                ReLoadData();
        }

        private void tsDel_Click(object sender, EventArgs e)
        {
            if (!MessageDialogHelper.ConfirmYesNo("确认删除？"))
                return;
            var entity = treeView1.SelectedNode.Tag as SYS_User;
            if (entity == null)
                return;
            if (SelectedType == SelectedType.User)
            {
                new UserService().Delete(entity.Id);
            }
            else
            {
                new DepartmentService().DeleteDepartment(entity.Id);
            }
            ReLoadData();
            MessageDialogHelper.ShowInfomation("删除成功");
        }

        private void tsRole_Click(object sender, EventArgs e)
        {
            var frm = new FrmSelectRole()
            {
                Data = treeView1.SelectedNode.Tag as SYS_User
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                ReLoadData();
            }
        }

        #endregion

    }
}
